/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ArticleEditor.StartMenu.actions;

import java.awt.GraphicsEnvironment;
import java.awt.event.KeyEvent;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import java.util.Collection;
import javax.swing.JTextPane;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.Highlighter;
import org.ArticleEditor.DataContent.ArticleEditorTopComponent;
import org.articleEditor.insertContent.ArticleTopComponent;
import org.articleEditor.view.Styleable;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.loaders.DataObject;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.pushingpixels.flamingo.api.ribbon.JFlowRibbonBand;
import org.pushingpixels.flamingo.api.ribbon.JRibbon;
import org.pushingpixels.flamingo.api.ribbon.JRibbonComponent;
import org.pushingpixels.flamingo.api.ribbon.JRibbonFrame;

/**
 *
 * @author edil
 */

public class formato extends JFlowRibbonBand implements LookupListener{
    
    DataObject Dobj;
    private Styleable styleable;
    private Lookup.Result<DataObject> result = null;
    private Lookup.Result<Styleable> resultStyle = null;
    private JTextPane textPane;
    boolean bandera=false;
    public  Highlighter highlighter = new UnderlineHighlighter(null);
    private  static javax.swing.JComboBox namesFont;
    private static javax.swing.JComboBox sizeFont;
    
     public formato() {
        super("Font", null);
               
        initComponents();
        SetFonts();
        SetSizeFonts();        
        JRibbonComponent fontComboWrapper = new JRibbonComponent(namesFont);
        addFlowComponent(fontComboWrapper);    
       
      
        //JRibbonComponent sizeComboWrapper = new JRibbonComponent(sizeFont);
        //addFlowComponent(sizeComboWrapper);
     }

      private void initComponents() { 
        
        namesFont = new javax.swing.JComboBox();
        sizeFont = new javax.swing.JComboBox();
        namesFont.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                namesFontMouseMoved(evt);
            }
        });
        namesFont.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                namesFontActionPerformed(evt);
            }
        });
        namesFont.addKeyListener(new java.awt.event.KeyAdapter() {
            @Override
            public void keyPressed(java.awt.event.KeyEvent evt) {
                namesFontKeyPressed(evt);
            }
        });

        sizeFont.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        sizeFont.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                sizeFontMouseMoved(evt);
            }
        });
        sizeFont.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sizeFontActionPerformed(evt);
            }
        });
        sizeFont.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                sizeFontKeyPressed(evt);
            }
        });
        
        
      }
       private void SetFonts()
    {        
      GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
      String[] font = env.getAvailableFontFamilyNames();      
      namesFont.setModel(new javax.swing.DefaultComboBoxModel(font));    
    
    }
    
     private void SetSizeFonts()
    {      
      String[] size = {"8","9","10","11","12","14","16","18","20","22","24","26","28","30","48","60","72"};      
      sizeFont.setModel(new javax.swing.DefaultComboBoxModel(size));
      sizeFont.setEditable(true);
    }
     
      private void namesFontActionPerformed(java.awt.event.ActionEvent evt) {   
           
        if ( bandera==true)
        {
            listener();
            styleable=ArticleEditorTopComponent.findCurrent(); 
            String fontName=(String) namesFont.getSelectedItem();
            AttributedString attributes = new AttributedString("ChangeFont");
            if (fontName != null && styleable!=null) {           
             attributes.addAttribute(TextAttribute.FAMILY, fontName);
             styleable.setFontAttributes(attributes);
            }  
        }
    }                                         

    private void namesFontKeyPressed(java.awt.event.KeyEvent evt) {                                     
       if(evt.getKeyCode() == KeyEvent.VK_ENTER)
       {
           listener();
           styleable=ArticleEditorTopComponent.findCurrent(); 
           String fontName=(String) namesFont.getSelectedItem();
           AttributedString attributes = new AttributedString("ChangeFont");
           if (fontName != null && styleable!=null) {           
            attributes.addAttribute(TextAttribute.FAMILY, fontName);
            styleable.setFontAttributes(attributes);
        }
       }
    }                                    

    private void sizeFontActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
        if ( bandera==true)
        {
            
           styleable=ArticleEditorTopComponent.findCurrent();            
           String tam= (String)sizeFont.getSelectedItem();
           AttributedString attributes = new AttributedString("FontSize");
           if (tam != null && styleable!=null) {  
            Integer num=new Integer(Integer.parseInt(tam));
            Number number=(Number)num;
           attributes.addAttribute(TextAttribute.SIZE, number);
           styleable.setFontAttributes(attributes);
           }
        }        
    }                                        

    private void sizeFontKeyPressed(java.awt.event.KeyEvent evt) {                                    
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
       {
           styleable=ArticleEditorTopComponent.findCurrent(); 
           String tam= (String)sizeFont.getSelectedItem();
           AttributedString attributes = new AttributedString("FontSize");
           if (tam != null && styleable!=null) {           
           Integer num=new Integer(Integer.parseInt(tam));
           Number number=(Number)num;
           attributes.addAttribute(TextAttribute.SIZE, number);
            styleable.setFontAttributes(attributes);
        }
       }
    }                                   

    private void namesFontMouseMoved(java.awt.event.MouseEvent evt) {                                     
       bandera=true;
    }                                    

    private void sizeFontMouseMoved(java.awt.event.MouseEvent evt) {                                    
        bandera=true;
    }               
     public void listener()
     {
        styleable=ArticleEditorTopComponent.findCurrent(); 
        textPane=styleable.getTextPanel(); 
        //textPane.setHighlighter(highlighter);
        //search();
         //listenerBuscar();
         textPane.addCaretListener(new CaretListener(){

            @Override
            public void caretUpdate(CaretEvent e) {                   
                    String name=styleable.getFont();
                    int size=styleable.getFontSize();                       
                    bandera=false; 
                    namesFont.setSelectedItem(name);
                    sizeFont.setSelectedItem(Integer.toString(size));
            }
        });       
     }
     
    @Override
    public void resultChanged(LookupEvent le) {          
        Collection<? extends DataObject> allEvents = result.allInstances();
        Collection<? extends Styleable> allEventsStyle = resultStyle.allInstances();
        
        if (!allEvents.isEmpty()) {
            DataObject event = allEvents.iterator().next();            
            Dobj = event;
                if(!allEventsStyle.isEmpty()){
                    //System.out.println("Styleable no está vacio");
                    styleable = allEventsStyle.iterator().next();                   
                    listener();
                }
            
        } else {
            System.out.println("--");
        }
    }
}
